package fun.asgc.boot.netty.context;

import fun.asgc.boot.netty.base.event.SimpleApplicationEventManager;
import fun.asgc.boot.netty.util.ArrayUtil;
import fun.asgc.boot.netty.util.LockUtil;
import fun.asgc.boot.netty.util.StringUtil;
import fun.asgc.boot.netty.util.SystemUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author: aoshiguchen
 * @date: 2022/6/16
 */
@Accessors(chain = true)
@Data
public class Environment {
	/**
	 * 启动类
	 */
	private Class<?> mainClass;
	/**
	 * 启动参数
	 */
	private String[] mainArgs;
	/**
	 * 包扫描路径
	 */
	private List<String> scanBasePackages;
	/**
	 * 横幅
	 */
	private String banner;
	/**
	 * 应用配置
	 */
	private ApplicationConfig config;
	/**
	 * 启用job
	 */
	private boolean enableJob;
	/**
	 * 运行上下文
	 */
	private SystemUtil.RunContext runContext;
	/**
	 * 默认的应用事件管理器
	 */
	private SimpleApplicationEventManager defaultApplicationEventManager;
	/**
	 * 参数map
	 */
	private Map<String, String> mainArgsMap = null;

	private Map<String, String> getMainArgsMap() {
		return LockUtil.doubleCheckProcessForNoException(
				() -> null == mainArgsMap,
				this,
				() -> {
					Map<String, String> map = new HashMap<>();
					if (ArrayUtil.notEmpty(mainArgs)) {
						for (String param : mainArgs) {
							if (!StringUtil.isEmpty(param)) {
								int index = param.indexOf("=");
								if (index > 0 && index < param.length() - 1) {
									String key = param.substring(0, index);
									String val = param.substring(index + 1);
									map.put(key, val);
								}
							}
						}
					}
					mainArgsMap = map;
				},
				() -> mainArgsMap
		);
	}

	private String getMainArgs(String key) {
		return getMainArgsMap().get(key);
	}

	public Integer getMainArgsForInteger(String key, Integer defaultValue) {
		String val = getMainArgs(key);
		Integer res = null;
		try {
			if (!StringUtil.isEmpty(val)) {
				res = Integer.valueOf(val);
			}
		} catch (Exception e) {
			// ignore
		}
		return (null != res) ? res : defaultValue;
	}

	public Boolean getMainArgsForBoolean(String key, Boolean defaultValue) {
		String val = getMainArgs(key);
		Boolean res = null;
		try {
			if (!StringUtil.isEmpty(val)) {
				res = Boolean.valueOf(val);
			}
		} catch (Exception e) {
			// ignore
		}
		return (null != res) ? res : defaultValue;
	}

	public String getMainArgsForString(String key, String defaultValue) {
		String res = getMainArgs(key);
		return !StringUtil.isEmpty(res) ? res : defaultValue;
	}

	public String getMainArgsForString(String key) {
		return getMainArgsForString(key, null);
	}
}
